 	program testefcn

        include 'param_dim.inc'

      	integer m,numpar,ldfjac	
	integer maxfev,mode,nprint,info,nfev,njev
	integer ipvt(npar_max)
	double precision ftol,xtol,gtol,factor

	double precision p(npar_max),s(npar_max)
	double precision x(npar_max),fvec(NRCX),fjac(NRCX,npar_max),
     *        diag(npar_max), qtf(npar_max), wa1(npar_max),
     *        wa2(npar_max), wa3(npar_max), wa4(NRCX)
	double precision epsfcn

        double precision fvec1(NRCX)

	double precision fjacDD(NRCX*npar_max),fjacDF(NRCX*npar_max)
	double precision errodif(npar_max),normadif


        double precision r,dudp(Nparg_max),h
	integer count1,count2,count_rate,count_max

	integer FDAT,FDCC,FDEX,FRES,FROT,f_old,f_new
	CHARACTER*11 FILEB	
	CHARACTER*15 FILE_DAT,FILE_DEX,FILE_OPT,FILE_RES
	character*18 FILEPLD

	common/geom/p,s,numpc
	COMMON /narq/ FDAT,FDCC,FDEX,FRES,FROT
	COMMON /cFDAT/ FILE_DAT,FILE_DEX,FILE_OPT,FILE_RES
! 	external fcn
	external fcn1,fcn2



	  ireffinal=2
	  do irefin=1,ireffinal
	  

	      call input_opt(ftol,xtol,gtol,maxfev,numpar,
     &           m,metodo,mode,nprint,factor,epsfcn,irefin)
		  ldfjac = NRCX

	    
	    write(*,*) '-------refinam. time = ',irefin
	    do i=1,numpc
		write(*,*)p(i*2-1),p(i*2),s(i)
	    enddo
	    
	    if(numpar .eq. numpc)then
	      do i=1,numpar
		!x(i)=s(i)
		x(i)=tan((s(i)-0.5)*3.1416)
	      enddo
	    else if(numpar .eq. 2*numpc)then
	      do i=1,numpar
		x(i)=p(i)
	      enddo
	    else if(numpar .eq. 3*numpc)then
	      do i=1,numpc
		x(3*i-2)=p(2*i-1)
		x(3*i-1)=p( 2*i )
		!x(3*i  )=s(  i  )
		x(3*i  )=tan((s(i)-0.5)*3.1416)
	      enddo
	    endif
		  
		  
		  

	    if(metodo.eq.1)then

	      write(4,*)'_______________________________________'
	      write(4,*)'--L-M - J:Finite Differences--'
	      write(4,*)'_______________________________________'

	    call system_clock(count1,count_rate,count_max)

	    call lmdifmodif(fcn1,m,numpar,x,fvec,ftol,xtol,gtol,maxfev,
     *            epsfcn,diag,mode,factor,nprint,info,nfev,fjac,ldfjac,
     *                ipvt,qtf,wa1,wa2,wa3,wa4)
            call system_clock(count2,count_rate,count_max)
            r=(count2-count1)/count_rate

            write(4,*)'Fim da primeira etapa da otimizacao'
            write(4,*)'Tempo de resol. (1a etapa) c/ LMDIF:',r,' s'
            write(4,*)'info: ',info,'  nfev: ',nfev
	  else if(metodo.eq.2)then

            write(4,*)'_______________________________________'
            write(4,*)'--L-M - J:DirectDifferentiation--'
            write(4,*)'_______________________________________'

            call system_clock(count1,count_rate,count_max)

            call lmder(fcn2,m,numpar,x,fvec,fjac,ldfjac,ftol,xtol,gtol,
     *                 maxfev,diag,mode,factor,nprint,info,nfev,njev,
     *                 ipvt,qtf,wa1,wa2,wa3,wa4)
            call system_clock(count2,count_rate,count_max)
            r=(count2-count1)/count_rate

            write(4,*)'Tempo de resol. (1a etapa) c/ LMDER:',r,' s'
            write(4,*)'info: ',info,'  nfev: ',nfev,'  njev:',njev
	  endif




	if(irefin.ne.ireffinal)then
	
	  f_old=10
	  open(f_old,file='auxiliar_old.tmp')
	  write(f_old,*)numpc
	  do i=1,numpc
	    write(f_old,*)x(2*i-1),x(2*i  ),s(i)
	  enddo
	  close(f_old)
	  
	  call system('~/Documents/local_codes/tie2D-incl-refina/refinE')
	  

	endif
	
	enddo ! fim do numero de execucoes para refinamento sucessivo.



        !  2o passo da otimizacao   - + Parametro S 
	if(numpar .eq. 2*numpc)then
            write(4,*)'************************************'
            write(4,*)' 2o passo da otimizacao - x y s '
            write(4,*)'************************************'

!             xtol = xtol/2.
!             ftol = ftol/2.
!             gtol = gtol/2.


            do i=1,numpar
                p(i)=x(i)
            enddo
            
	
            numpar = 3*numpc

		write(4,*)
		write(4,*)'  Parametros do alg. de otimizacao'
		write(4,*)
		write(4,*)'   ftol: ',ftol
		write(4,*)'   xtol: ',xtol
		write(4,*)'   gtol: ',gtol
		write(4,*)'   maxfev: ',maxfev
		write(4,*)'   numpar: ',numpar		

            do i=1,numpc
                x(3*i-2)=p(2*i-1)
                x(3*i-1)=p( 2*i )
                x(3*i  )=tan((s(i)-0.5)*3.1416)
            enddo
    
            if(metodo.eq.1)then

                write(4,*)'_______________________________________'
                write(4,*)'--L-M - J:Finite Differences--'
                write(4,*)'_______________________________________'


                call system_clock(count1,count_rate,count_max)

                call lmdifmodif(fcn1,m,numpar,x,fvec,ftol,xtol,gtol,
     *                   maxfev,epsfcn,diag,mode,factor,nprint,info,
     *                   nfev,fjac,ldfjac,ipvt,qtf,wa1,wa2,wa3,wa4)
                call system_clock(count2,count_rate,count_max)
                r=(count2-count1)/count_rate

                write(4,*)'Tempo de resol. (2a etapa) c/ LMDIF:',r,' s'
                write(4,*)'info: ',info,'  nfev: ',nfev

            else if(metodo.eq.2)then

                write(4,*)'_______________________________________'
                write(4,*)'--L-M - J:DirectDifferentiation--'
                write(4,*)'_______________________________________'   

                call system_clock(count1,count_rate,count_max)

                call lmder(fcn2,m,numpar,x,fvec,fjac,ldfjac,ftol,xtol,
     *                 gtol,maxfev,diag,mode,factor,nprint,info,nfev,
     *                 njev,ipvt,qtf,wa1,wa2,wa3,wa4)
                call system_clock(count2,count_rate,count_max)
                r=(count2-count1)/count_rate

                write(4,*)'Tempo de resol. (2a etapa)  c/ LMDER:',r,' s'
                write(4,*)'info: ',info,'  nfev: ',nfev,'  njev:',njev

            endif	

        endif !fim da segunda parte da otimizacao

 5	format(i5,54f10.5)

	close(4) ! arquivo xxxx.dop.out
	close(FRES)


	
	open(1,file='pgeom.tmp')
	read(1,*)FILEPLD
	read(1,*)g1,g2,g3,g4,g5,g6,g7
	close(1)
	
	open(1,file='fobj0.tmp')
	read(1,*)fobj0
	close(1)
	
	open(1,file='fobjf.tmp')
	read(1,*)fobjf
	close(1)
	
	open(1,file='auxerro.tmp')
	FILEB=FILE_DEX
	write(1,*)FILEB//'-00.pld' !alvo
	write(1,*)FILEPLD !resultado	
	close(1)
	
	
	
	call system('~/Documents/local_codes/erro-curvas/erro-geom')
	
	open(1,file='errog'//FILEPLD//'.tmp')
	read(1,*)errogeom
	close(1)
	
	open(1,file='snr.tmp')
	read(1,*)snr
	close(1)
	
	open(1,file='resum-'//FILE_OPT//'.txt')
	write(1,*)snr,g1,g2,g3,g4,g5,g6,g7,fobj0,fobjf,errogeom
	write(1,*)'snr,x,y,dc,area,perim,Ju,Jv,fobj0,fobjf,errogeom'
	close(1)
	
	call system('mv '//FILEPLD//' resultpld.tmp')!troca o nome dos arqs
	call system('mv '//FILEB//'-00.pld alvopld.tmp')	
	call system('rm *.pld ')
	call system('rm *.plt ')
	call system('mv resultpld.tmp '//FILEPLD) !retorna os arqs pros nomes originais
	call system('mv alvopld.tmp '//FILEB//'-00.pld')	
	
	
	call system('gzip -f '//FILE_RES)
	call system('rm '//FILE_RES//'.gz') !opcional: remove o .gz, para economizar espaço em disco (so para dados com ruido)
	call system('rm *.tmp ')


	stop
	end

